package com.sensePlusVR.realGame;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;

public class AudioPlayer {
	private static final String TAG="AudioPlayer";
	public static final int AUDIO_EVENT_STOPED = 0;
	private  MediaPlayer mp;
	private String audio;
	private OnCompletionListener listener;
	private OnPreparedListener preListener;
	private boolean loop;
	private boolean startWithQuit;
	private float volume;
	private EventListener EventCallback;
	
	public void setOnEventListener(EventListener l){
		this.EventCallback = l;
	}
	
	public AudioPlayer(float volume, boolean startWithQuit){
		this(volume);
		this.startWithQuit = startWithQuit;
	}
	
	public AudioPlayer(float volume){
		this.volume = volume;
		mp = new MediaPlayer();
		mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
		loop = false;
		listener = new OnCompletionListener(){

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				//mp.stop();
				if (EventCallback != null){
					EventCallback.onEvent(AUDIO_EVENT_STOPED, null);
				}
//				if (loop)
//					play(audio, loop);
			}
		};
		preListener = new OnMyPreparedListener(volume, startWithQuit);
		mp.setOnCompletionListener(listener);
		mp.setOnPreparedListener(preListener);
	}
	
	private class OnMyPreparedListener implements OnPreparedListener{
		private float volume;
		private boolean startWithQuit;
		public OnMyPreparedListener(float volume, boolean startWithQuit){
			this.volume = volume;
			this.startWithQuit = startWithQuit;
		}
		@Override
		public void onPrepared(MediaPlayer mp) {
			// TODO Auto-generated method stub
			if (startWithQuit){
				mp.setVolume(0, 0);
			}else{
				Log.e(TAG,"set volume="+volume);
				mp.setVolume(volume, volume);	
			}
            mp.start();    //开始播放             			
		}
		
	}
	
	public void play(String audio, boolean loop){
		if (audio == null || audio.isEmpty())
			return;
		Log.e(TAG, "play: "+audio);
		if (mp != null) {
			try {
				this.loop = loop;
				this.audio = audio;
				mp.reset();
				mp.setDataSource(audio);
				mp.prepare();
				mp.setLooping(loop);
				//mp.seekTo(0);
				//mp.start();
			} catch (Exception e) {
				Log.d("hint", "can't jump pre music");
				e.printStackTrace();
			}
			
		}
	}
	
	public void setVolume(float v){
		if (mp.isPlaying())
			mp.setVolume(v, v);
		this.volume = v;
	}
	
	public int getRemain(){
		int p = mp.getCurrentPosition();
		int d = mp.getDuration();
		if (d != -1){
			return (d - p);
		}
		return p;
	}
	
	public int getDuration(){
		return mp.getDuration();
	}
	
	public boolean isPlaying(){
		return  mp.isPlaying();
	}
	
	public void play(String audio){
		play(audio, false);
	}
	
	public void stopFade(float time){
		int looptime = (int)time/50;
		new fadeThread(looptime, volume).start();
	}
	
	public class fadeThread extends Thread{
		private int loop;
		private float volume;
		fadeThread(int loop, float curv){
			this.loop = loop;
			volume = curv;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			float step = volume/loop;
			while(loop-- >= 0){
				if (mp.isPlaying()){
					volume = volume - step;
					if (volume < 0.0f){
						break;
					}
					mp.setVolume(volume, volume);
					Log.e(TAG, "set volume="+volume);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if (mp.isPlaying()){
				mp.stop();
			}
		}		
	}
	
	public void stop(){
		loop = false;
		mp.stop();
	}
	public void release(){
		mp.release();
	}
	public void pause(){
		mp.pause();
	}
	public void resume(){
		mp.start();
	}
	public interface EventListener {
		void onEvent(int event, Object args);
	}
}
